<?php
/**
 * 
 * User/IndexAction.class.php (前台会员中心模块)
 *
 * @package      	jgsat
 * @author          liuxun QQ:147613338 <admin@jgsat.cn>
 * @copyright     	Copyright (c) 2008-2011  (http://www.jgsat.com)
 * @license         http://www.jgsat.com/license.txt
 * @version        	jgsat企业网站管理系统 v2.1 2012-10-08 jgsat.cn $
 */
if(!defined("JGsat")) exit("Access Denied");
class UserAction extends BaseAction
{

	function _initialize()
    {	
		parent::_initialize();

      //  echo ($this->_userid);exit;
		if(!$this->_userid){
            header("Location: ".U('Mobile/Login/index'));
			exit;
		}


		$this->dao = M('User');
		$this->assign('bcid',0);
		$user = $this->dao->find($this->_userid);
        $this->user=$user;
		$this->assign('vo',$user);
        $this->assign('title','餐饮空间_个人中心');
		unset($_POST['status']);
		unset($_POST['groupid']);
		unset($_POST['amount']);
		unset($_POST['point']);
		$_GET =get_safe_replace($_GET);
        
    }

    public function index()
    {
        $user= $this->user;
         $this->display();


    }




    //设计师个人资料
    public function designer()
    {

        if($_POST['id']){
            $_POST['id']=$this->_userid;

            if(!$this->dao->create($_POST)) {
                $this->error($this->dao->getError());
            }
            $this->dao->update_time = time();
            $this->dao->last_ip = get_client_ip();
            $this->dao->avatar=tran($_REQUEST['avatar']);
           // print_r($_POST['itemType']);exit;
            foreach($_POST['itemType'] as $k=>$v){
                if ($k == 0) {
                    $this->dao->itemType= '';
                }else{$r=',';}
             $this->dao->itemType.=$r.$v;
            }
            $result	=	$this->dao->save();

            if(false !== $result) {
                $this->success(L('do_success'));
            }else{
                $this->error(L('do_error'));
            }
            exit;
        }

        $this->assign('menuWZ',1);

        $this->display();
    }



    //业主个人资料
    public function guestbook()
    {

        if($_POST['id']){
            $_POST['id']=$this->_userid;

            if(!$this->dao->create($_POST)) {
                $this->error($this->dao->getError());
            }
            $this->dao->update_time = time();
            $this->dao->last_ip = get_client_ip();
            $this->dao->avatar=tran($_REQUEST['avatar']);
             //print_r($_POST);exit;

            $result	=	$this->dao->save();
            //echo $this->dao->getLastSql();exit;

            if(false !== $result) {
                $this->success(L('do_success'));
            }else{
                $this->error(L('do_error'));
            }
            exit;
        }

        $this->assign('menuWZ',1);

        $this->display();
    }

    public function product()
    {

        if($_POST['id']){
            $_POST['id']=$this->_userid;

            if(!$this->dao->create($_POST)) {
                $this->error($this->dao->getError());
            }
            $this->dao->update_time = time();
            $this->dao->last_ip = get_client_ip();
            $this->dao->avatar=tran($_REQUEST['avatar']);
            //print_r($_POST);exit;

            $result	=	$this->dao->save();

            if(false !== $result) {
                $this->success(L('do_success'));
            }else{
                $this->error(L('do_error'));
            }
            exit;
        }

        $this->assign('menuWZ',1);

        $this->display();
    }



    //修改密码
    public function password()
    {
        if($_POST['dosubmit']){
            if(md5($_POST['verify']) != $_SESSION['verify']) {
                $this->error(L('error_verify'));
            }
            if($_POST['password'] != $_POST['repassword']){
                $this->error(L('password_repassword'));
            }
            $map	=	array();
            $map['password']= array('eq',sysmd5($_POST['oldpassword']));
            $map['id']		=	$this->_userid;
            //检查用户
            if(!$this->dao->where($map)->field('id')->find()) {
                $this->error(L('error_oldpassword'));
            }else {
                $this->dao->id = $this->_userid;
                $this->dao->update_time = time();
                $this->dao->password	=	sysmd5($_POST['password']);
                $r = $this->dao->save();
                $this->assign('jumpUrl',U('User/Index/password'));
                if($r){
                    $this->success(L('do_success'));
                }else{
                    $this->error(L('do_error'));
                }
            }
            exit;
        }
        $this->assign('menuWZ',3);

        $this->display();
    }
	


    //添加评论
    public function comment_post()
    {
        $this->dao=M('Comment');
        if(empty($_POST['content'])) $this->error("内容不能为空!");
        $_POST['content']=safe_replace($_POST['content']);
        if(!$this->dao->create($_POST)) {
            $this->error($this->dao->getError());
        }

		$id=$_POST['conid'];
		$module=$_POST['contype'];
        $title=$_POST['title'];
        $catid=$_POST['catid'];
        if($module=='User'){
            $user=M('User')->field('realname')->where("id=".$id)->find();
            $this->dao->sjsname=$user['realname'];
            $title='【设计师】 '.$title;
        }
        if($module=='Case'){
            if($catid=='1') {
                $title = '【案例图】 ' . $title;
            }
            if($catid=='2'){
                $title = '【效果图】 ' . $title;
            }
        }
        if($module=='Product'){
            $title='【餐饮设备】 '.$title;
        }

        //添加用户动态
        $userdate['title']='评论了'.$title;
        $userdate['url']=$_POST['url'];
        $userdate['userid']=$this->_userid;
        $userdate['conid']=$id;
		$userdate['module']=$module;
        $userdate['createtime']=time();
        $userdate['lang']='1';
        $userdate['status']='1';
        $ud=M("Dynamic")->add($userdate);



        $this->dao->userid = $this->_userid;
        $this->dao->username = $this->_username;
        $this->dao->title=$title;
        $this->dao->createtime = time();
        $this->dao->updatetime = time();
        $this->dao->status = 1;
        $this->dao->lang = 1;
        $result	=$this->dao->add();
        if(false !== $result) {            
            $this->assign ( 'jumpUrl', $_POST["url"] );
			M($module)->where("id=".$id)->setInc('com_num'); //添加评论次数

            $this->success(L('评论成功'));
        }else{
            $this->error("评论发布失败,请认真尝试!");
        }
        exit;
    }

    //评价，喜欢讨厌
    public function eva_post(){

        $module=safe_replace($_GET['mm']);
        $id=safe_replace($_GET['id']);
        if($_GET['like']){
            $eva='like_num';
          }
        if($_GET['haet']){
            $eva='haet_num';
        }

        $this->dao=M('User_eva');
        $where="userid=".$this->_userid." and module='".$module."' and conid=".$id;
        $arr=$this->dao->where( $where)->find();
       if($arr){

           $this->error("您已经评价过了哦");
       }
	   
	     
		 
		 
	   
	   
        $this->dao->userid = $this->_userid;
        $this->dao->module =$module;
        $this->dao->conid=$id;
        $this->dao->add(); //添加记录

        $m=M($module);
        $relut=$m->where("id=".$id)->setInc($eva); //添加次数
        if($relut){
            $this->success("操作成功!");

        }


    }

    public function keep(){
        //echo 1;exit;
        $module=safe_replace($_POST['module']);
        $id=safe_replace($_POST['id']);
        $url=$_POST['url'];
        $title=safe_replace($_POST['title']);
        $catid=safe_replace($_POST['catid']);
        //echo $module;exit;
        $this->dao=M('Keep');

        $where="userid=".$this->_userid." and module='".$module."' and conid=".$id;
        //echo $where;exit;
        $arr=$this->dao->where( $where)->find();
        if($arr){
            echo "您已经收藏过了哦";exit;
        }

        if($module=='User'){
            $user=M('User')->field('realname')->where("id=".$id)->find();

            $title='【设计师】 '.$user['realname'];
        }
        if($module=='Case'){
            if($catid=='1') {
                $title = '【案例图】 ' . $title;
                $this->dao->catid=1;
            }
            if($catid=='2'){
                $title = '【效果图】 ' . $title;
                $this->dao->catid=2;
            }
        }
        if($module=='Product'){
            $title='【餐饮设备】 '.$title;
        }
        if($module=='Zforum_theme'){
            $title='【帖子】 '.$title;
        }

        //添加用户动态
        $userdate['title']='收藏了'.$title;
        $userdate['url']=$url;
        $userdate['userid']=$this->_userid;
        $userdate['conid']=$id;
        $userdate['module']=$module;
        $userdate['createtime']=time();
        $userdate['lang']='1';
        $userdate['status']='1';
        $ud=M("Dynamic")->add($userdate);




        $data['userid'] = $this->_userid;
        $data['module'] =$module;
        $data['conid']=$id;
        $data['url']=$url;
        $data['title']=$title;
        $data['createtime']=time();
        $data['sjsname']=$user['realname'];
//echo $this->_userid.'Y'.$module.'Y'.$id.'Y'.$url.'Y'.$title;exit;
        $this->dao->add($data); //添加记录


        $m=M($module);
        $relut=$m->where("id=".$id)->setInc('keep_num'); //添加次数
        if($relut){
            echo '收藏成功!';

        }


    }



    //我的评论
    public function comment(){

        $this->_userid;
        $count = M('Comment')->where("userid=".$this->_userid)->count();

        if($count){
            import ( "@.ORG.Page" );
            $listRows =  10;
            $page = new Page ( $count, $listRows );
            $pages = $page->preshow();
            // $field =  $this->module[$this->mod[$module]]['listfields'];
            $field =  "*";
            $list = M('Comment')->field($field)->where("userid=".$this->_userid)->order("id desc")->limit($page->firstRow . ',' . $page->listRows)->select();

            $this->assign('pages',$pages);
            $this->assign('list',$list);
        }
        // print_r($list);exit;

        //$this->assign('Categorys', $this->Categorys);
        $this->assign('menuWZ',13);

        $this->display();


    }

    //删除评论
    function delcomment(){
        $id=intval($_GET['id']);
        if($id){
            $this->dao = M('Comment');
            $list=$this->dao->field('userid')->where("id=".$id)->find();
            if($this->_userid!=$list['userid']){
                $this->error(L('出错啦'));
                exit;
            }}
        $result=M('Comment')->where("id=".$id)->delete();

        if($result){
            $this->success('操作成功', $this->url);
            }


    }



    function mykeep(){

       //$result=M('Keep')->where("userid=".$this->_userid)->select();
        $module="'Zforum_theme'";
        if($_REQUEST['module']){
            $module="'".$_REQUEST['module']."'";
        }
        $where="module=".$module;
        $where.=" and userid=".$this->_userid;
        if($_REQUEST['catid']){
            $where.=" and catid=".$_REQUEST['catid'];
        }

        $count  =M('Keep')->where($where)->count();

        if($count){
            import ( "@.ORG.Page" );
            $listRows =  8;
            $page = new Page ( $count, $listRows );
            $pages = $page->preshow();
            // $field =  $this->module[$this->mod[$module]]['listfields'];
            $field =  "*";

            $list = M('Keep')->field($field)->where($where)->order("id desc")->limit($page->firstRow . ',' . $page->listRows)->select();
           // print_r($list);exit;
            $this->assign('pages',$pages);
            $this->assign('list',$list);
        }



        $this->assign('list',$list);
        $this->display();


    }
//删除评论
    function delkeep(){
        $id=intval($_GET['id']);
        if($id){
            $this->dao = M('Keep');
            $list=$this->dao->field('userid')->where("id=".$id)->find();
            if($this->_userid!=$list['userid']){
                $this->error(L('出错啦'));
                exit;
            }}
        $result=M('Keep')->where("id=".$id)->delete();

        if($result){
            $this->success('操作成功', $this->url);
        }


    }

	function checkmobile(){
            $mobile=$_GET['mobile'];
            if($this->dao->where("mobile='".$mobile."' and id<>".$this->_userid)->find()){
                echo 'false';
            }else{
                echo 'true';
            }
            exit;
	}
        
	function checknickname(){
            $nickname=$_GET['nickname'];
            if($this->dao->where("nickname='".$nickname."' and id<>".$this->_userid)->find()){
                echo 'false';
            }else{
                echo 'true';
            }
            exit;
	}
        
	function checkEmail(){
            $email=$_GET['email'];
            if($this->dao->where("email='{$email}' and id<>".$this->_userid)->find()){
                echo 'false';
            }else{
                echo 'true';
            }
            exit;
	}

    public function mobile_email()
    {

        $this->assign('bcid',0);//顶级栏目
        // $this->assign('ishome','home');
        $this->display();
    }

    //绑定邮件
    function addemail(){
        $email=safe_replace($_POST['mail']);
        $verify=safe_replace($_POST['verify']);
        // echo $email;$verify;exit;
        $userid=$this->_userid;
        $data['email']= $email;

        if( cookie('email_verify')==md5($verify) && $email==cookie('email_aa')){
            $r=M('User')->where('id='.$userid)->save($data);
            if($r) {
                $this->success("绑定邮箱成功!",'index.php?g=Mobile&m=User&a=index');
            }else{
                $this->error("绑定失败，未知错误");
            }
        }else{
            $this->error("绑定失败，验证码或邮箱错误，或验证码已过期");
        }

    }
//删除邮箱
    function delemail(){
        $email=safe_replace($_POST['mail']);
        $verify=safe_replace($_POST['verify']);
        // print_r($_POST);exit;
        //echo $email.$verify;exit;
        $userid=$this->_userid;
        $data['email']= '';

        if( cookie('email_verify')==md5($verify) && $email==cookie('email_aa')){
            $r=M('User')->where('id='.$userid)->save($data);
            if($r) {
                $this->success("邮箱解绑成功!",'index.php?g=Mobile&m=User&a=index');
            }else{
                $this->error("解绑失败，未知错误");
            }
        }else{
            $this->error("解绑失败，验证码错误，或验证码已过期");
        }

    }

    //添加发送邮件
    function s_email(){

        $email=safe_replace($_POST['mail']);


            if($this->dao->getByEmail($email)){
                echo '邮箱已存在';
            }else{

         $verify=rand(10000000,99999999);
                // session_start();

                cookie('email_verify',$verify,600);
                cookie('email_aa',$email,600);
         $message = str_replace(array('{verify}','{sitename}'),array($verify,$this->Config['site_name']),$this->member_config['member_emailchecktpl']);

        $r = sendmail($email,L('USER_REGISTER_CHECKEMAIL').'-'.$this->Config['site_name'],$message,$this->Config);
        if($r){
            echo '邮件已经发送';
        }


            }



    }
//发现邮箱 修改
    function email(){

        $email=safe_replace($_POST['mail']);




        $verify=rand(10000000,99999999);
        // session_start();

        cookie('email_verify',md5($verify),600);
        cookie('email_aa',$email,600);
        $message = str_replace(array('{verify}','{sitename}'),array($verify,$this->Config['site_name']),$this->member_config['member_emailchecktpl']);

        $r = sendmail($email,L('USER_REGISTER_CHECKEMAIL').'-'.$this->Config['site_name'],$message,$this->Config);
        if($r){
            echo '邮件已经发送';
        }
    }
    public function expoint()
    {
        if($_POST['dosubmit']){
            if(!M("Exchange")->create($_REQUEST)) {
            $this->error(M("Exchange")->getError());
            }
            M("Exchange")->userid =  $this->_userid;
            M("Exchange")->username =  $this->_username;
            M("Exchange")->status = 0;
            M("Exchange")->lang =1;
            M("Exchange")->createtime = time();
            $result	=	M("Exchange")->add();
            if(false !== $result) {
                $this->success("提交成功!");
            }else{
                $this->error("提交失败,请重新尝试");
            }
            exit;
        }
        
        
        $this->assign('menuWZ',26);
        $this->display();
    }
    //站内信
    public function message()
    {
        $this->assign('title','站内信');
        $groupid=$this->user['groupid'];
        $where="(touserid=".$this->_userid." or usergroup=4 or usergroup=".$groupid.')';
        $where.=" and createtime>=".cookie('createtime');
			
		$count = M("User_message")->where($where)->count();
        
        if($count){
            import ( "@.ORG.Page" );
            $listRows =  10;
            $page = new Page ( $count, $listRows );
            $pages = $page->preshow();
            //$field =  $this->module[$this->mod[$module]]['listfields'];
            $field =  "*";
//echo $where;exit;

            if(empty($_GET['new'])){
            $list = M("User_message")->field($field)->where($where)->order("id desc")->limit($page->firstRow . ',' . $page->listRows)->select();
               // echo M("User_message")->getLastSql();exit;
            }
            if($_GET['new']==2){
                //已读
                $where.=" and find_in_set('$this->_userid', `read`)";
                 $list = M("User_message")->field($field)->where($where)->order("id desc")->limit($page->firstRow . ',' . $page->listRows)->select();

            }
            if($_GET['new']==1){
                //未读
                $where.=" and find_in_set('$this->_userid', `read`)=0";
                $list = M("User_message")->field($field)->where($where)->order("id desc")->limit($page->firstRow . ',' . $page->listRows)->select();
               // echo $Dao->getLastSql();exit;
            }
            $this->assign('pages',$pages);
            $this->assign('list',$list);
            $this->assign('userid',$this->_userid);
        }
        $this->assign('menuWZ',22);
        $this->assign('userid',$this->_userid);
        $this->display();

    }

    public function message_show()
    {

            $groupid=$this->user['groupid'];
            $id=intval($_REQUEST['id']);
            $list = M("User_message")->where("id=".$id)->find();
            if($list['touserid']!=$this->_userid && $list['usergroup']!=4 && $list['usergroup']!=$groupid){
                $this->error(L('没有权限'));
            }

                 $read=explode(",",$list['read']);
                 if(!in_array($this->_userid, $read)){
                 $data['read']=$list['read'].$this->_userid.',';
                 $re=M("User_message")->where("id=".$id)->save($data);

 }
//print_r($list);exit;
            $this->assign('list',$list);


        $this->display();

    }

    //站内信全部设为已读
function message_all(){
    $groupid=$this->user['groupid'];
    $where="(touserid=".$this->_userid." or usergroup=4 or usergroup=".$groupid.')';
    $where.=" and find_in_set('$this->_userid', `read`)=0";
    $list = M("User_message")->where($where)->select();
   foreach($list as $k=>$v){
       $data['read']=$v['read'].$this->_userid.',';
       $re=M("User_message")->where("id=".$v['id'])->save($data);
   }
    $this->success("操作成功!");
}


    public function pointdetail()
    {
        $count = M("User_detail")->where("userid=".$this->_userid)->count();
        if($count){
            import ( "@.ORG.Page" );
            $listRows =  10;
            $page = new Page ( $count, $listRows );
            $pages = $page->preshow();
            $field =  $this->module[$this->mod[$module]]['listfields'];
            $field =  "*";
            $list = M("User_detail")->field($field)->where("userid=".$this->_userid)->order("id desc")->limit($page->firstRow . ',' . $page->listRows)->select();
            $this->assign('pages',$pages);
            $this->assign('list',$list);
        }
        $this->assign('menuWZ',27);
        $this->display();
		
    }

    

    public function updateUserPwd()
    {
        $password=get_safe_replace($_POST['password']);
        $repwd=get_safe_replace($_POST['repwd']);
        if(empty($repwd)||empty($password)){
            $this->error("两次密码都不可为空!");
        }
        if($repwd != $password){
            $this->error("两次密码不一致!");
        }
        $data["password"]=sysmd5($password);
        $result = M("User")->where("id=".$this->_userid)->save($data);
        if(false !== $result) {
             $this->success("修改成功!");
         }else{
             $this->error("修改失败,请重新尝试");
         }
         exit;
     }
    



     public function updateUserMobile()
    {
        $mobile=get_safe_replace($_POST['mobile']);
        if(empty($mobile)){
            $this->error("手机号码不能为空!");
        }else {
            $mode="^0?(13[0-9]|15[012356789]|18[0236789]|14[57])[0-9]{8}$";
            if (!ereg($mode,$mobile)){
                 $this->error("您输入的手机号码不合法!");
            }
        }
        $data["mobile"]=$mobile;
        $result = M("User")->where("id=".$this->_userid)->save($data);
        if(false !== $result) {
             $this->success("手机号码修改成功!");
         }else{
             $this->error("修改失败,请重新尝试");
         }
         exit;
     }






}
?>